package com.example.starseas_phone;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.starseas_phone.Adapters.Alerts_listviewAdapter;
import com.example.starseas_phone.Entity.Alerts;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainF3 extends Fragment {

    private ListView mainf3_listview;
    private List<Alerts> list = new ArrayList<>();
    private List<Alerts> list1 = new ArrayList<>();
    private Alerts_listviewAdapter adapter;

    private FloatingActionButton mainf3_floatbtn;
    private Spinner mainf3_spinner;

    private List<String> NoticeNamelist =new ArrayList<String>();
    private String selectNoticeName="";
    private int a = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mainf3,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        initspinner();

        mainf3_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                list.clear();
                for (int j = 0; j < list1.size(); j++) {
                    if (list1.get(j).getNoticeName().equals(NoticeNamelist.get(i))){
                        list.add(list1.get(i));
                    }
                }
                adapter.notifyDataSetChanged();
                mainf3_listview.setAdapter(adapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        mainf3_floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),AddAlertsActivity.class);
                intent.putExtra("PositionId",Sto.teachers.getPositionId()+"");
                startActivity(intent);
            }
        });

    }

    private void initview(){
        mainf3_listview = (ListView) getActivity().findViewById(R.id.mainf3_listview);
        adapter = new Alerts_listviewAdapter(getActivity(),R.layout.alerts_listview,list);

        mainf3_floatbtn = (FloatingActionButton) getActivity().findViewById(R.id.mainf3_floatbtn);
        mainf3_spinner = (Spinner) getActivity().findViewById(R.id.mainf3_spinner);
    }

    private void Stuinit(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Alerts/GetAllAlertByToken?token="+ Sto.students.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String AlertsTitle = jsonObject1.getString("AlertsTitle");
                        String AlertsContent = jsonObject1.getString("AlertsContent");
                        String NoticeName = jsonObject1.getString("NoticeName");
                        String WorkerName = jsonObject1.getString("WorkerName");
                        int StudentShow = jsonObject1.getInt("StudentShow");
                        String AlterToApplyForTime = jsonObject1.getString("AlterToApplyForTime");
                        Alerts alerts = new Alerts(AlertsTitle,AlertsContent,NoticeName,WorkerName,StudentShow,AlterToApplyForTime);
                        list.add(alerts);
                        list1.add(alerts);
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mainf3_listview.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void Teainit(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Alerts/GetAllAlerts")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String AlertsTitle = jsonObject1.getString("AlertsTitle");
                        String AlertsContent = jsonObject1.getString("AlertsContent");
                        String NoticeName = jsonObject1.getString("NoticeName");
                        String WorkerName = jsonObject1.getString("WorkerName");
                        int StudentShow = jsonObject1.getInt("StudentShow");
                        String AlterToApplyForTime = jsonObject1.getString("AlterToApplyForTime");
                        Alerts alerts = new Alerts(AlertsTitle,AlertsContent,NoticeName,WorkerName,StudentShow,AlterToApplyForTime);
                        list.add(alerts);
                        list1.add(alerts);
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("list大小",list.size()+"");
                            mainf3_listview.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initspinner(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Notice/GetAllNotice")
                            .get()
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String message = jsonObject1.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String NoticeName = jsonObject.getString("NoticeName");
                        NoticeNamelist.add(NoticeName);
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.classid_item, R.id.text,NoticeNamelist);
                            mainf3_spinner.setAdapter(adapter);
                            if (Sto.State==1){
                                mainf3_floatbtn.setVisibility(View.GONE);
                                Stuinit();
                            }else {
                                mainf3_floatbtn.setVisibility(View.VISIBLE);
                                Teainit();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}